baselib.report(0x00, "TD4.7.6 beginning.")
-- 下面是初始化操作:
busilib.SetOrientation(1)             -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
local VCC1 = 0
local VCC2 = 0

test1To6()                            -- 第一到六步
-- 第七步:If PUT is a PUT_C with Ra on its plug’s Vconn pin:
-- 如果 PUT 是 PUT_C 并且 Ra 附加到其 Vconn 引脚:
-- 7.a:Complete steps 16 – 18.  7.b:Skip to step 10
VCC2 = busilib.GetADC(ADC.CC2, 4)
if PUT_C and VCC2 < 200 then
    goto Script16
end
-- 第八步:CVS toggles between Unattached.SNK and Unattached.Accessory for tDRPTry
-- CVS 在 tDRPTry 内在 Unattached.SNK 和 Unattached.Accessory 之间切换
-- 状态未定义且切换时间间隔？？？
-- 第九步:CVS verifies PUT attaches Rp after CVS transitions to Unattached.SNK for tPDDebounce [TD.4.7.6.V.3]
-- CVS 验证 PUT 在 CVS 转换到 Unattached.SNK 后 tPDDebounce 内附加 Rp
busilib.setUsbCcs(CCS.Unattached)        
busilib.setUsbRole(ROLE.Sink)
baselib.delay(20)                     -- 等待tPDDebounce(20ms)
VCC1 = busilib.GetADC(ADC.CC1, 4)
if VCC1 > 200 then
    baselib.report(0x00, "TD4.7.6.V.3 pass.")
else
    baselib.report(0x00, "TD4.7.6.V.3 fail.")
end
test10To12()                            -- 第10到12步
-- 第十三步:If the PUT is not a PUT_R the test is over.
if not PUT_R then
    goto ScriptEnd
end
-- 第十四步:CVS attaches Ra to one CC to appear as an Emarked Cabled for the remainder of the test
-- CVS将一个CC引脚设置为Ra，模拟电子标记电缆，持续测试
busilib.SetSwitchState(SW.CC2_RA, 1)       -- 将CC2_RA开关打开
-- 第十五步:Repeat steps 1-6
test1To6()

:: Script16 ::
-- 第十六步:CVS transitions to PoweredAccessory after max tCCDebounce
-- CVS 在 max tCCDebounce 后转换到 PoweredAccessory 状态
baselib.delay(200)
--busilib.setUsbCcs(CCS.PoweredAccessory)      现在应该没有 PoweredAccessory 状态
-- 第十七步:If PUT has VIF field Type_C_Is_Vconn_Powered_Accessory set to YES:
if Type_C_Is_Vconn_Powered_Accessory then
    -- 17.a步:CVS verifies that PUT responds to PD signaling [TD.4.7.6.V.9]
    -- 这步不会
    -- 17.b步:CVS transitions to Disabled for 750ms
    busilib.setUsbCcs(CCS.Disabled)
    baselib.delay(750)
    -- 17.c步:CVS transitions to Unattached.SNK
    busilib.setUsbCcs(CCS.Unattached)
    busilib.setUsbRole(ROLE.Sink)
else 
    -- 第十八步:Else Type_C_Is_Vconn_Powered_Accessory set to NO:
    -- 18.a步:CVS transitions to Try.SNK and starts tDRPTry
    -- 18.b步:CVS verifies PUT transitions to TryWait.SRC after tPDDebounce [TD.4.7.6.V.10]
    -- 这步目前写得了吗，缺少TryWait.SRC状态和Try.SNK状态
end
-- 第十九步:重复10-12步
test10To12()

:: ScriptEnd ::
baselib.report(0x00, "Demo is done here.")
baselib.report(0x07, "\x04")

function test1To6()
    -- 第一步:CVS transitions to Unattached.SNK
    busilib.setUsbCcs(CCS.Unattached)        
    busilib.setUsbRole(ROLE.Sink)
    -- 第二步:CVS 等待 5ms
    baselib.delay(5)
    -- 第三步:CVS monitors CCs for Rp and verifies that Rp is connected within max dcSRC.DRP x max tDRP. [TD.4.7.6.V.1]
    -- CVS 验证 PUT 在 max dcSRC.DRP × max tDRP 内附加 Rp
    baselib.delay(70)                     -- 等待max dcSRC.DRP x max tDRP  
    local tempVCC1 = busilib.GetADC(ADC.CC1, 4)
    baselib.report(0x00, "CC1 is " .. tempVCC1 .. "mV")
    if tempVCC1 > 200 then
        baselib.report(0x00, "TD4.7.6.V.1 pass.")
    else
        baselib.report(0x00, "TD4.7.6.V.1 fail.")
    end
    -- 第四步:CVS transitions to AttachWait.SNK
    -- 目前没这个状态
    -- 第五步:CVS verifies that PUT removes Rp and attaches Rd after tCCDebounce (transitions to Try.SNK) [TD.4.7.6.V.2]
    -- CVS 验证 PUT 转换到 Try.SNK ,(PUT tCCDebounce 后移除 Rp 并附加 Rd)
    baselib.delay(200)               -- tCCDebounce:max 200ms
    local tempVCC1 = busilib.GetADC(ADC.CC1, 4)
    baselib.report(0x00, "CC1 is " .. tempVCC1 .. "mV")
    if tempVCC1 < 200 then
        baselib.report(0x00, "TD4.7.6.V.2 pass.")
    else
        baselib.report(0x00, "TD4.7.6.V.2 fail.")
    end
    -- 第六步:CVS transitions to Unattached.SNK after max tPDDebounce, then Unattached.Accessory after min tDRP x min dcSRC.DRP
    baselib.delay(20)               -- tPDDebounce:max 20ms
    busilib.setUsbCcs(CCS.Unattached)
    baselib.delay(15)               -- min tDRP(50ms) x min dcSRC.DRP(0.3)
    -- 这里CVS应该是Unattached.Accessory状态，目前没有这个状态
end

function test10To12()
    -- 第十步:CVS verifies PUT and CVS transition to Attached.SRC and Attached.SNK after tPDDebounce:
    -- 第10.a步:PUT sources Vbus within tVbusOn [TD.4.7.6.V.4]
    -- PUT 在 tVBUSON 内提供 VBUS
    baselib.delay(275)               -- tVBUSON:275ms
    if cstmlib.VBusVerified(1) then 
        baselib.report(0x00, "TD4.7.6.V.4 pass.")
    else
        baselib.report(0x00, "TD4.7.6.V.4 fail.")
    end
    -- 第10.b步:If PUT supports PD messaging:PUT and CVS negotiate an explicit contract [TD.4.7.6.V.5]
    if USB_PD_Support then
        baselib.report(0x00, "Begin power negotiation.")
        tcpclib.SetPermission(1, 2)
        PN_result = tcpclib.PresetPowerNegotiation(1, 500)  --timeout is 500ms
        if PN_result == LuaOk then
            baselib.report(0x00, "TD4.7.6.V.5 pass.")
        else
            if PN_result == LuaTimeout then
                baselib.report(0x00, "Power negotiation timeout.")
            else
                baselib.report(0x00, "Unsupported power request received.")
            end
            baselib.report(0x00, "TD4.7.6.V.5 fail.")
        end
    end
    -- 10.d步:If PUT supports USB 2.0,PUT starts USB communications on its D+/D- pair [TD.4.7.6.V.7]
    -- 先放空
    -- 第十一步:CVS transitions to Disabled for 750ms
    busilib.setUsbCcs(CCS.Disabled)
    baselib.delay(750)
    -- 第十二步:CVS verifies PUT transitions to Unattached.SNK before tVBUSOFF expires:PUT data communication has ceased [TD.4.7.5.V.8]
    -- data communication has ceased怎么验证？
end
